package com.detection.motion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.detection.motion.bean.DeviceGps;
import com.detection.motion.mapper.DeviceGpsMapper;
import com.detection.motion.service.DeviceGpsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 实现DeviceGpsService接口，调用DeviceGpsMapper方法实现数据库增删改查
 */
@Service
public class DeviceGpsServiceImpl extends ServiceImpl<DeviceGpsMapper, DeviceGps> implements DeviceGpsService {

    @Autowired
    DeviceGpsMapper deviceGpsMapper;

    @Override
    public void insertDeviceGps(DeviceGps deviceGps) {
        deviceGpsMapper.insertDeviceGps(deviceGps.getDeviceId(),
                deviceGps.getLongitude(),
                deviceGps.getLatitude(),
                deviceGps.getGradient(),
                deviceGps.getShakeNum(),
                deviceGps.getXNum(),
                deviceGps.getYNum(),
                deviceGps.getZNum(),
                deviceGps.getLoryty(),
                deviceGps.getRemix(),
                deviceGps.getP(),
                deviceGps.getHumidity(),
                deviceGps.getTemp(),
                deviceGps.getCo2(),
                deviceGps.getVoc(),
                deviceGps.getMlux());
    }

    @Override
    public ArrayList<DeviceGps> selectDeviceGpsBytimeSection(Integer deviceId, String startTime, String endTime) {
        return deviceGpsMapper.selectDeviceGpsBytimeSection(deviceId,startTime,endTime);
    }

    @Override
    public DeviceGps getLastInfo(Integer deviceId) {
        return deviceGpsMapper.getLastInfo(deviceId);
    }

    @Override
    public DeviceGps getLastGpsInfo(Integer deviceId) {
        List<DeviceGps> deviceGpsInfos = deviceGpsMapper.selectList(new QueryWrapper<DeviceGps>()
                .eq("device_id", deviceId));
//        System.out.println(deviceGpsInfos);
        List<DeviceGps> deviceGpsList = deviceGpsInfos.stream().filter(deviceGps -> {
            return deviceGps.getLatitude() != null && deviceGps.getLongitude() != null;
        }).collect(Collectors.toList());
//        System.out.println(deviceGpsList);

        return deviceGpsList.get(deviceGpsList.size()-1);
    }
}